//
//  XmlParserCategoryList.m
//  FleaFinder
//
//  Created by Hieu Le on 4/8/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import "XmlParserCategoryList.h"

@implementation XmlParserCategoryList

-(id)init
{
    self = [super init];
    
    if(self != nil){
        tagCategoryList = @"categorylist";
        tagCategory = @"category";
        tagId = @"id";
        tagTitle = @"title";
        
        parsedObjects = [NSMutableArray arrayWithCapacity:10];
        addedVisAlle = NO;
        isInsideTagCategory = NO;
    }
    
    return self;
}

-(NSMutableArray*) getParsedObjects
{
    if(!addedVisAlle){
        Category *visAlle = [[Category alloc] init];
        visAlle.categoryId = -1;
        visAlle.title = @"Vis Alle";
        
        [parsedObjects addObject:visAlle];
        addedVisAlle = YES;
    }

    return parsedObjects;
}

-(void) parseXML:(NSString *)sourceUrl
{
    NSURL *url = [[NSURL alloc] initWithString: sourceUrl];
    
    NSError *error;
    NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
    [xmlParser setDelegate:self];
    [xmlParser parse];
}

#pragma implementation of NSXMLParserDelegate

-(void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    //NSLog(@"Error = %@", parseError);
}

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
    if(parsedObjects != nil){
        [parsedObjects removeAllObjects];
    }
}

-(void) parserDidEndDocument:(NSXMLParser *)parser
{
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([tagCategory compare:elementName] == NSOrderedSame) {
        
        currentCategory = [[Category alloc] init];
        isInsideTagCategory = YES;
        
    } else if([tagId compare:elementName] == NSOrderedSame){
        currentTag = CATEGORY_TAG_ID;
    } else if([tagTitle compare:elementName] == NSOrderedSame){
        currentTag = CATEGORY_TAG_TITLE;
    }
    
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([tagCategory compare:elementName] == NSOrderedSame){
        [parsedObjects addObject:currentCategory];
        currentCategory = nil;
        isInsideTagCategory = NO;
    }
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([string rangeOfString:@"\n"].location != NSNotFound){
        return;
    }
    
    if(isInsideTagCategory){
        if(currentTag == CATEGORY_TAG_ID){
            currentCategory.categoryId = [string intValue];
        } else if(currentTag == CATEGORY_TAG_TITLE){
            if(currentCategory.title != nil){
                currentCategory.title = [NSString stringWithFormat:@"%@%@", currentCategory.title, string];
            } else {
                currentCategory.title = string;
            }
        }
    }
}


@end
